package com.carltechnology.mahjonggamecaculation.dao;

import com.carltechnology.mahjonggamecaculation.base.MyAbstractSubscriber;
import com.carltechnology.mahjonggamecaculation.dao.impl.MahjongGameInsertDataComponent;
import com.carltechnology.mahjonggamecaculation.mode.IDetailGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameRuntimeData;
import com.carltechnology.mahjonggamecaculation.utils.Utils;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class MahjongInsertHelper {
    private IMahjongGameInsertComponent insertComponent;
    public MahjongInsertHelper(IMahjongDataDao dao) {
        insertComponent = new MahjongGameInsertDataComponent(dao);
    }

    public void insertMahjongGameData(MyAbstractSubscriber<IGameData> subscriber, final IGameData mode){
        getInsertGameData(mode).subscribe(subscriber);
    }

    public void insertMahjongDetailGameData(MyAbstractSubscriber<IDetailGameData> subscriber,final IDetailGameData mode){
        getInsertDetailData(mode).subscribe(subscriber);
    }

    public void insetMahjongRuntimeGameData(MyAbstractSubscriber<IGameRuntimeData> subscriber,final IGameRuntimeData mode){
        getInsertRuntimeData(mode).subscribe(subscriber);
    }

    public void insertRuntimeList(MyAbstractSubscriber<List<IGameRuntimeData>> subscriber,final List<IGameRuntimeData> list){
        getInsertRuntimeDataList(list).subscribe(subscriber);
    }
    private Observable<IGameData> getInsertGameData(final IGameData mode){
        return Observable.create(new Observable.OnSubscribe<IGameData>() {
            @Override
            public void call(Subscriber<? super IGameData> subscriber) {
                insertComponent.insertMahjongGameData(mode);
                subscriber.onNext(mode);
                subscriber.unsubscribe();
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SMALL_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    private Observable<IDetailGameData> getInsertDetailData(final IDetailGameData mode){
        return Observable.create(new Observable.OnSubscribe<IDetailGameData>() {
            @Override
            public void call(Subscriber<? super IDetailGameData> subscriber) {
                insertComponent.insertMahjongDetailGameData(mode);
                subscriber.onNext(mode);
                subscriber.unsubscribe();
            }
        });
    }
    private Observable<IGameRuntimeData> getInsertRuntimeData(final IGameRuntimeData mode){
        return Observable.create(new Observable.OnSubscribe<IGameRuntimeData>() {
            @Override
            public void call(Subscriber<? super IGameRuntimeData> subscriber) {
                insertComponent.insertRuntimeGameData(mode);
                subscriber.onNext(mode);
                subscriber.unsubscribe();
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SMALL_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    private Observable<List<IGameRuntimeData>> getInsertRuntimeDataList(final List<IGameRuntimeData> list){
        return Observable.create(new Observable.OnSubscribe<List<IGameRuntimeData>>() {
            @Override
            public void call(Subscriber<? super List<IGameRuntimeData>> subscriber) {
                insertComponent.insertRunningModeList(list);
                subscriber.onNext(list);
                subscriber.unsubscribe();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
